package com.lhl.jwt.token;

import io.swagger.annotations.Api;
import lombok.Getter;
import lombok.Setter;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;

import java.util.Collection;

/**
 * @className: com.lhl.jwt.token.WxAuthenticationToken
 * @description: 微信通用token
 * @author: king
 * @date: 2020-12-30 10:03
 **/
@Api(tags = "WxAuthenticationToken", produces = "WxAuthenticationToken")
@Getter
@Setter
public class WxAuthenticationToken extends AbstractAuthenticationToken {
    // 企业号:userId,公众号,小程序:openId,公众平台:unionId
    private String wxId;
    private String mobile;
    //企业微信:cp,公众号:mp,小程序:miniApp
    private String type;

    public WxAuthenticationToken(String wxId, String mobile, String type) {
        super(null);
        this.wxId = wxId;
        this.mobile = mobile;
        this.type = type;
    }

    public WxAuthenticationToken(String wxId, String mobile, String type,Collection<? extends GrantedAuthority> authorities) {
        super(authorities);
        this.wxId = wxId;
        this.mobile = mobile;
        this.type = type;
    }
    public WxAuthenticationToken(String wxId, Collection<? extends GrantedAuthority> authorities) {
        super(authorities);
        this.wxId = wxId;
        super.setAuthenticated(true);
    }
    public Object getCredentials() {
        return this.mobile;
    }

    public Object getPrincipal() {
        return this.wxId;
    }

    public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
        if (isAuthenticated) {
            throw new IllegalArgumentException(
                    "Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead");
        }

        super.setAuthenticated(false);
    }

    @Override
    public void eraseCredentials() {
        super.eraseCredentials();
    }
}
